package com.gzw.dms.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.adapter.MessageAdapter;
import com.gzw.dms.adapter.QscxAdapter1;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.MessageInfo;
import com.gzw.dms.bean.ShippingOrderInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;
import com.gzw.dms.view.SwipeRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener{
    private Toolbar toolbar;
    private Context mContext;
    private MessageAdapter mAdapter;
    private List<MessageInfo> lsMapInfo = new ArrayList<>();
    private List<ShippingOrderInfo> shippingOrderInfo;
    private Intent mIntent;
    private String  state = "";
    private  String fpstste="1";
    private SwipeRefreshLayout mSwipeLayout;
    private RecyclerView mRecyclerView;
    enum RefreshOrLoad {
        REFRESH, LOAD
    }
    private RefreshOrLoad refreshOrLoad = RefreshOrLoad.REFRESH;
    private String page = "10";
    private String row = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_message);

    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        mRecyclerView = (RecyclerView) findViewById(R.id.agreement_listView);
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("消息");
        mIntent = getIntent();
        setSupportActionBar(toolbar);

        mRecyclerView.setHasFixedSize(true);
        //设置布局管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //设置Item增加、移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //添加默认分割线
//        mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.HORIZONTAL));
        //添加自定义分割线
        //mRecyclerView.addItemDecoration(new RecycleViewDivider(
        //mContext, LinearLayoutManager.HORIZONTAL, R.drawable.divider_bg));
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setOnLoadListener(this);
        mSwipeLayout.setColor(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        mSwipeLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeLayout.setLoadNoFull(false);
        ShowOrderTask showOrderTask = new ShowOrderTask();
        showOrderTask.execute();
    }

    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new myOnClick());

    }

    @Override
    public void onRefresh() {
        row = "0";
        refreshOrLoad = RefreshOrLoad.REFRESH;
        ShowOrderTask showOrderTask = new ShowOrderTask();
        showOrderTask.execute();
    }
    @Override
    public void onLoad() {
        row = String.valueOf(mAdapter.getItemCount() + 1);
        refreshOrLoad = RefreshOrLoad.LOAD;
        ShowOrderTask showOrderTask = new ShowOrderTask();
        showOrderTask.execute();
    }
    class myOnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {

                default:

                    Intent mIntent = new Intent();
                    mIntent.setClass(mContext, HomeActivity.class);
                    ComponentName componentName = mIntent.resolveActivity(getPackageManager());
                    if (componentName == null) {
                        // 说明系统中不存在这个activity\
                        mIntent.setClass(mContext, HomeActivity.class);
                        startActivity(mIntent);

                    }else{
                        Intent mIntent2 = new Intent();
                        //把返回数据存入Intent
                        mIntent2.putExtra("result", 1);
                        //设置返回数据
                        setResult(3, mIntent2);
                        finish();
                    }

                    break;
            }
        }
    }

    public class ShowOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {

            if (refreshOrLoad == RefreshOrLoad.REFRESH) {
                mSwipeLayout.setRefreshing(true);
            } else {
                mSwipeLayout.setLoading(true);
            }
        }

        protected String doInBackground(String... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("page", page);
            map.put("row", row);
            map.put("userInfo", userInfo);
            map.put("state","1");
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appGetMsgInfo"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            if (refreshOrLoad == RefreshOrLoad.REFRESH) {
                mSwipeLayout.setRefreshing(false);
            } else {
                mSwipeLayout.setLoading(false);
            }
            if (result == null || result.length() == 0) {
                T.showShort(mContext, "连接服务器异常。");
            }
            if (result.indexOf("successMsg") > 0) {
                String successMsg = JsonUtil.getMsg(result, "successMsg");
                final List<MessageInfo> lsMap = JsonUtil.getPersons(successMsg, new TypeToken<List<MessageInfo>>() {
                }.getType());
                if (refreshOrLoad == RefreshOrLoad.REFRESH) {
                    lsMapInfo.clear();
                    lsMapInfo.addAll(lsMap);
                } else {
                    lsMapInfo.addAll(lsMap);
                }
                if (mAdapter == null) {
                    mRecyclerView.setAdapter(mAdapter = new MessageAdapter(lsMapInfo));
                } else {
                    mAdapter.notifyDataSetChanged();
                }
                mAdapter.setOnRecyclerViewListener(new MessageAdapter.OnRecyclerViewListener() {
                    @Override
                    public void onItemClick(int position) {
                        MessageInfo shippingOrder =  lsMapInfo.get(position);
                        Intent mIntetn = new Intent();
                        Bundle bundle = new Bundle();
                        mIntetn.setClass(mContext, MessageDetailActivity.class);
                        bundle.putSerializable("messageInfo", shippingOrder);
                        mIntetn.putExtras(bundle);
                        startActivityForResult(mIntetn, 1);
                    }
                    @Override
                    public boolean onItemLongClick(int position) {
                        return false;
                    }
                });
            } else if (result.indexOf("errorMsg") > 0) {
                T.showShort(mContext, JsonUtil.getMsg(result, "errorMsg"));
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu_home; this adds items to the action bar if it is present.;
//        getMenuInflater().inflate(R.menu.menu_vehicle, menu);
        getMenuInflater().inflate(R.menu.menu_back, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_back:
                Intent myIntetn = new Intent();
                myIntetn.setClass(mContext, SendMessageActivity.class);
                startActivityForResult(myIntetn, 1);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
